import { EmployeeService } from "./../src/service/employeeService";
import assert = require("assert");
import { DataContextFactory } from "../src/domain/fbsDataContext";
import { Employee } from "../src/domain/model/employee";
import { Guid } from "../src/common/index";

async function clearData() {
    let ctx = DataContextFactory.GetDataContext();
    let employeeList = await ctx.Employee.ToList();
    for (let item of employeeList) {
        await ctx.Delete(item);
    }
}

async function initData() {

    let ctx = DataContextFactory.GetDataContext();
    let waiterRole = {
        id: Guid.GetGuid(),
        name: "waiter",
        desc: "string",
        checked: true
    };

    let busserRole = {
        id: Guid.GetGuid(),
        name: "busser",
        desc: "string",
        checked: true
    };

    let cashierRole = {
        id: "2",
        name: "cashier",
        desc: "string",
        checked: true
    };

    // 构造employ 
    for (let i = 0; i <= 50; i++) {
        let employee = new Employee()
        employee.id = Guid.GetGuid();
        employee.employeeNumber = "22";
        employee.storeId = "2a8mmqsc5kw0k4g00co4gko0c";
        employee.account = "18583920602";
        let rNo = i % 4;
        employee.roles = [];
        switch (rNo) {
            case 0: break;
            case 1: employee.roles = [busserRole]; break;
            case 2: employee.roles = [busserRole, cashierRole]; break;
            case 3: employee.roles = [busserRole, cashierRole, waiterRole]; break;
        }

        await ctx.Create(employee);
    }
}

describe("EmployeeService Test", function () {
    let employeeService = new EmployeeService();
    describe("getEmployeeByRole", function () {
        describe("无员工数据", () => {
            before(async () => {
                await clearData();
            })
            it("获取服务员", async () => {
                let waiterList = await employeeService.getEmployeeByRole("waiter");
                assert.equal(waiterList.length, 0, "waiterList.length != 0");
            })
            it("获取清台员", async () => {
                let waiterList = await employeeService.getEmployeeByRole("busser");
                assert.equal(waiterList.length, 0, "waiterList.length != 0");
            })
            it("传入空参数", async () => {
                let waiterList = await employeeService.getEmployeeByRole("");
                assert.equal(waiterList.length, 0, "waiterList.length != 0");
            })
            it("传入不存在角色", async () => {
                let waiterList = await employeeService.getEmployeeByRole("yzw");
                assert.equal(waiterList.length, 0, "waiterList.length != 0");
            })
        })
        describe("有员工数据", () => {
            before(async () => {
                await initData();
            })
            it("获取服务员", async () => {
                let waiterList = await employeeService.getEmployeeByRole("waiter");
                assert.equal(waiterList.length, 12, "waiterList.length != 12");
            })
            it("获取清台员", async () => {
                let waiterList = await employeeService.getEmployeeByRole("busser");
                assert.equal(waiterList.length, 38, "waiterList.length != 38");
            })
            it("传入空参数", async () => {
                let waiterList = await employeeService.getEmployeeByRole("");
                assert.equal(waiterList.length, 0, "waiterList.length != 0");
            })
            it("传入不存在角色", async () => {
                let waiterList = await employeeService.getEmployeeByRole("yzw");
                assert.equal(waiterList.length, 0, "waiterList.length != 0");
            })
        })
    });
})